package com.jeeboot.modules.app.controller;



import com.jeeboot.modules.app.service.UserService;
import com.jeeboot.modules.app.utils.JwtUtils;
import com.jeeboot.system.common.utils.R;
import com.jeeboot.system.common.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by IntelliJ IDEA.
 * 作者: 李哲训
 * 邮箱：370261528@qq.com
 * QQ群：571785017
 * 时间: 2017/11/29
 * 功能:APP登录授权
 */


@RestController
@RequestMapping("/app")
public class ApiLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 登录
     */
    @PostMapping("login")
    public R login(String mobile, String password){
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        //用户登录
        long userId = userService.login(mobile, password);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

}
